package com.carboy.launch.receiver;

import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;

import com.carboy.launch.TtsManager;
import com.carboy.launch.utils.Constants;
import com.carboy.launch.utils.JwdLog;
import com.carboy.launch.utils.Tool;
import com.carboy.launch.widgets.CustomToast;
import com.jwd.clouddog.broadcast.JwdServerBroadCast;

/**
 * 
 * @author James
 * @Package com.huixin.carnav.receiver
 * @Title: VoiceBroadcast.java
 * @Description 振动报警
 * @date 2015-10-14
 * @version 1.0
 * 
 */
public class VoiceBroadcast extends BroadcastReceiver {

	@Override
	public void onReceive(final Context context, Intent intent) {
		String aciton = intent.getAction();

		if (aciton.equals("android.intent.action.VOICE.BROADCAST")) {
			String text = intent.getStringExtra("text");
			if (!Constants.isVoiceDvr) {
				if (text.equals("蓝牙已连接")) {
					if (Constants.firstBroad) {
						TtsManager.getInstance(context).playText(text);
						Constants.firstBroad = false;
					}

				} else {
					TtsManager.getInstance(context).playText2(text);
					Constants.isVoiceDvr = false;
				}

			}
		}
		// 检测振动
		if (aciton.equals(Constants.ACTION_SEND_ANTI_THEFT)
				&& Constants.openBurglar) {
			JwdLog.playVoice(context, "检测到振动");
			// 标记振动过
			Constants.isShake = true;
			Date mDate = new Date();
			Constants.mDate = mDate;
			// 启动车宝
			// JwdServerDateMethod.bindCloudService(context);
			boolean b = Tool.startService(context, "com.jwd.clouddog.service",
					"com.jwd.clouddog.StartClouddogService");
			// 发送振动报警,延迟10秒在发送，防止用户收到消息就去获取多媒体文件获取不到
			// DVR传递路径最快也需要43秒
			// 两次振动检测间隔一分钟，一次流程走完，一分钟后在继续检测
			Handler mHander = new Handler();
			mHander.postDelayed(new Runnable() {

				@Override
				public void run() {
					JwdLog.playVoice(context, "上传振动到微信");
					JwdServerBroadCast.sentServerBurglarAlarmRequestCast(
							context, 1);
				}
			}, 40000);
		}

	}
}
